// pages/articleList/articleList.js
//获取应用实例
const app = getApp();
var _page = 1;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sessionkey:'',
    userinfo:{},
    articList:{},
    article_data:[],
    promptText: true,
    httpPath: app.globalData.imagePath
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let _token = wx.getStorageSync("token");
    that.setData({
      sessionkey: _token
    });
    wx.showLoading({
      title: '数据加载中'
    })
    that.getArticlelist();
  },
  //获取列表信息
  getArticlelist:function(){
    let that = this; 
    let _token = that.data.sessionkey;
    //获取个人信息
    wx.request({
      url: app.globalData.httpsPath + '/user/info',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token
      },
      //请求后台数据成功  
      success: function (res) {
        wx.hideLoading();
        let _info = res.data.data;
        that.setData({
          userinfo: _info
        })
      }
    })
    //获取动态列表
    wx.request({
      url: app.globalData.httpsPath + '/article/list', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
      },
      data: {
        token: _token
      },
      success(res) {
        let _list = res.data.data;
        let _list_data = res.data.data.data;
        
        for (let i in _list_data){
          _list_data[i].contents = _list_data[i].contents.split('&hc').join('\n');//转换回车字符

          if (_list_data[i].pics != null){
            let picimg = _list_data[i].pics.split(",");
            _list_data[i].pics = picimg ;
          }
        }
        that.setData({
          articList: _list,
          article_data: _list_data
        })

      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    if (options.from === 'button') {
      // 来自页面内转发按钮
      console.log(options.target)
    }
    return {
      title: '绿茵圆小程序',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //删除动态
  delArticle:function(option){
    let that = this ; 
    let _token = that.data.sessionkey;
    let dataset = option.target.dataset;
    let _Index = dataset.id; //拿到是第几个数组
    let _article = that.data.article_data[_Index];//通过第几个获取数据
    let _id = that.data.article_data[_Index].id;

    wx.request({
      url: app.globalData.httpsPath + '/article/del',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token,
        article_id:_id
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res.data);
        wx.showToast({
          title: '删除成功！',
        })
        setTimeout(function () {
          that.getArticlelist();
        }, 300)
      }
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    let that = this;
    let _token = that.data.sessionkey;
    _page = 1;
    //获取动态列表
    wx.request({
      url: app.globalData.httpsPath + '/article/list?page=' + _page, // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
      },
      data: {
        token: _token
      },
      success(res) {
        let _list = res.data.data;
        let _list_data = res.data.data.data;
        for (let i in _list_data) {
          if (_list_data[i].pics != null) {
            let picimg = _list_data[i].pics.split(",");
            _list_data[i].pics = picimg;
          }
        }
        that.setData({
          articList: _list,
          article_data: _list_data
        })
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();

      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    let _token = that.data.sessionkey;
    _page += 1;
    // 显示加载图标
    wx.showLoading({
      title: '数据加载中',
    })
    wx.request({
      url: app.globalData.httpsPath + '/article/list?page=' + _page, // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json', // 默认值
      },
      data: {
        token: _token
      },
      //请求后台数据成功  
      success: function (res) {
        
        let _list = res.data.data;
        let oldcourt_list = that.data.article_data;
        
        if (_list.data.length > 0){
          for (let i = 0; i < _list.data.length; i++) {
            oldcourt_list.push(_list.data[i]);
            if (_list.data[i].pics != null) {
              let picimg = _list.data[i].pics.split(",");
              _list.data[i].pics = picimg;
            }
          }
          console.info(_list)
          //将获取的数据保存
          that.setData({
            articList: _list,
            article_data: oldcourt_list,
          })

        }else{
          that.setData({
            promptText:false
          })
        }
        // 隐藏加载框
        wx.hideLoading();
      }
    })

  },
  //预览图片
  previewImage: function (event) {
    let that = this;
    let httpurl = that.data.httpPath;

    let imgs = event.currentTarget.dataset.imgs;
    let temp = [];
    for (let index in imgs) {
      temp = temp.concat(httpurl + imgs[index]);
    }
    wx.previewImage({
      current: httpurl + event.currentTarget.dataset.src,
      urls: temp,
    })
  },
})